package com.atguigu.gmall.payment;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: 李旭
 * @create: 2021-07-26 14:43
 **/
public class AliaySdkDemo {

    public static void main(String[] args) {
        //Aliay客户端
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                "2016102100732915",
                "MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQCfks5V9WW7J9uFBijiVQhEE+pb92APrEDecZ5dhRrslVsudupnfh6t0tSpI+DBqk5VqHIv4LWyK7/Xn3FPIyAe2R803xCN9vdOlM0tvOwB+cnRZsc5krkCkcFnT73tMmWcq1nVLOGyFq+sYNZs3jwV8NGixo/Gf2cTeVuipWGlzIpKt7HP4TTlc8YHJXpCqJdrBeZgmDRHleCRXGbrEUqMyqjwQfY6wtjWCXza0JQJ53RchZjDVBEaLvxkx9c2CHZ97eyDvcqE2L8zr8eukzcWfvfy0VDBx/yMSbAN8VBHSHAyUJoFjUXjJ5hEIpPtvulyPiED1DEgVytLM3YRgnnPAgMBAAECggEBAI5CWMT1Qz54qBGNjkFIwVe/0X0vqmoOjL5C2IZTNc+Yz3M6/HpLS5p3jLNFkwfUhoWC6FmaoSE4M41NTbzZebdGpbWC78VAWPWrXsfZ1yVjxPJ0CERhc6LAwoUR6CZgeYoUkL+lhYg5BWBN7Jud/D8rNBlDn8W8qu4HkRQNEGK36bRkPQ98RXV18IAVfsOP1Up1rkg2LM27eJVGSlucyS70Y7DOrjtEsxADkeOtz1cctjorvJHPf3IKdfWVJt0WXxxKtO08U9o5Soneoi1HM0wuFA0Ck+erhP/N5nbqAVeEBzcKocVm1lB3wOiiKhqmu9CNOgLYCi2mmNts68ugjYECgYEA9Q4jIMV5vJ4dwZ/23DFdd7JskqT+PhcQvzp+MZ8BwovfoCEaLqKSN1kUOv5tgriOq+3lhZF+rDzU6Nil8LG3CP6pD5l5TDlQAg/gNFkwpblJLrT1pGUfwzkiAWPhsTidZxPuoAOYxeu3WHKKU3QY9V5kmgccLJq7cTOU3X2hx+8CgYEAprNOAevA2/zeaHzag1pFXNxdu+JZsk9diV9+9lwuE4zeXmTzyG1eaI8hU/OTZq9d//yx3q28iTdt7w6hI/ap+G9kDf+HyAbBX0fbi762ghntxxcCCx4ZWG2vpHpVnu2N/bjIWFE5efd/4HkiUmsvbcaJN/pITLS3SzYL4HW/jCECgYEAjYkoyNjFwycf0KFvlyX78hJfmyMopPv1dD14fXh1L8RR2M7FrIHBKa6NfLVO0t19qdexZT/vhdNstT/MOASszsknJBdkc532k/pPaGzLKCyoFA34gobVnY/2EA98XztvzgU5IbpyFn5aWv9s3V6Eu9NVD5FjuG92K5+7L2JMeokCgYEAgb8zaxlUraS+cvMinHPpYuJ97lu51qghNGfoiNWsYzkUZsIKYKiHf7hPh2khBB5genry8k+lW+ORpCxtzXESUDRa2u5Vf6GKZp+mSLlykgaeZIQK6h5OOd59g/ZXp/BbwOxOuzbVFpJFrLB7v0x/yOBrMu+R0Goj890oF1dudCECgYEAhc7S0LPs0YNxY+b9KWrv+h7uMZaTWbQEmPdiLGSekHSClR3/bn5DEkcgmwkEvSt8nuz7V0t+BocIYafbix86ol4swvGWHo6RuXbcseCE9LlZW9ic2vqTC7cwXzSYH43ln0yZCmoZU0DMSiEf0oZzqltnYmCuHTCWCU03PrJ+JgI=",
                "json",
                "UTF-8",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv4FgjWsfvbXEkzs5T+z5r4X9m5Qh8OA0zlZA3T9yeCqlcRRqydMBfRsB4V9aHUH0KCV875mzF7Feh3VoDilcDpGtuKo5l7YRVnGSyQTq0pQjMFYi/wKGFGHAA37Wl2L/AMaGpT6CJncSqcAJssY61qmz/o9lw+hRq6KBfdktl4xm0d5f+uMgCLKqa6bYbFyG7vNZszGJ6yW2n6fFbZ0B4PgPXfrRFVG+OS204bi7sGDba6DuJPPuk8B/gkVoDkmKHNRnLXZi74jyZROlEXKr8JElZqpT6RODkKAmIRCGlQrAadUntgmymguRlN2K+TfSNIigY1FRekb/BudYhlbA5QIDAQAB",
                "RSA2");

        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();

        Map<String,String> params = new HashMap<>();
        params.put("out_trade_no","sdfsatwreryteghuytrghuytrgthuytre");
        params.put("product_code","FAST_INSTANT_TRADE_PAY");
        params.put("total_amount","10");
        params.put("subject","我为了测试而付钱");
        request.setBizContent(JSONObject.toJSONString(params));
        AlipayTradePagePayResponse response = null;
        try {
            response = alipayClient.pageExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
            System.out.println(response.getBody());
        } else {
            System.out.println("调用失败");
            System.out.println(response.getBody());
        }

/*<form name="punchout_form" method="post" action="https://openapi.alipaydev.com/gateway.do?charset=UTF-8&method=alipay.trade.page.pay&sign=cv8yVWzSqsK2joIiCtM0qPfzsTswtPyBAPS0b9SyExPZlf6%2BqELUTdcaY60UW8ARxDWrkbReKdNEFyqVRHeZqSPHZ%2F%2BydwbG%2FcMAGUaZm93oAjJhqiaxSMxa8uolbLOPHFOWo1CU8Xe8EXB8%2B09KM3V6rWw1lovxx%2FHoNzYt7nNtBXN88zverp02gSIPPVi426obGc4%2FDoVbFcZo%2FmletjlGRnaudI7B2Yu0dmS4WQB%2BKruE4aIVz0WlHt8WRNrDaKSHdJA07w6VJ%2Fj2diJ5Jr5A1G6bNCDlp2JL0V8%2FrPhJ%2FaHPaA7mbT0e1jssuQ%2Fz4lmMqGYUHjLEZFN6vW0sWQ%3D%3D&version=1.0&app_id=2016102100732915&sign_type=RSA2&timestamp=2021-07-26+15%3A14%3A01&alipay_sdk=alipay-sdk-java-4.8.73.ALL&format=json">
<input type="hidden" name="biz_content" value="{&quot;out_trade_no&quot;:&quot;sdfsatwreryteghuytrghuytrgthuytre&quot;,&quot;total_amount&quot;:&quot;10&quot;,&quot;subject&quot;:&quot;我为了测试而付钱&quot;,&quot;product_code&quot;:&quot;FAST_INSTANT_TRADE_PAY&quot;}">
<input type="submit" value="立即支付" style="display:none" >
</form>
<script>document.forms[0].submit();</script>*/

    }
}
